package registnet.base.source;

import registnet.base.Course;

/**
 * Defines a class for creating a <code>Course</code> from the data from the database.
 */
public class CourseSource implements ICourseSource{
	
	/**
	 * The course.
	 */
	private Course course;
	
	/**
	 * The course code of the course from the database. 
	 */
	private String courseCode;
	
	/**
	 * Creates a course from a course from the database with the given course code.
	 * @param courseCode
	 */
	public CourseSource(String courseCode){
		this.courseCode = courseCode;
		load();
	}

	@Override
	public Course getCourse() {
		return course;
	}

	@Override
	public Course load() {
		
		if (course != null) return course;
		else course = new Course();
		
		try {
			registnet.dao.Course daoCourse = new registnet.dao.CoursePeer().getCourse(courseCode);
			
			//courseCode
			course.setCourseCode(courseCode);
			
			//courseName
			course.setCourseName(daoCourse.getCourseName());
			
			//unit
			course.setUnit(daoCourse.getUnits());
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	
		return course;
	}

}
